package com.zh.common.view

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.ColorFilter
import android.graphics.Paint
import android.graphics.PixelFormat
import android.graphics.Rect
import android.graphics.RectF
import android.graphics.drawable.Drawable

/**
 * 圆角Drawable
 */
class RoundedCornersDrawable(backgroundColor: Int, cornerRadius: Int) : Drawable() {
    private val mRect = RectF()
    private val mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private var mCornerRadius = 10 // 圆角半径
    private var mBackgroundColor = Color.WHITE // 背景颜色

    init {
        mBackgroundColor = backgroundColor
        mCornerRadius = cornerRadius
        mPaint.color = mBackgroundColor
    }

    override fun onBoundsChange(bounds: Rect) {
        super.onBoundsChange(bounds)
        mRect.set(bounds)
    }

    override fun draw(canvas: Canvas) {
        canvas.drawRoundRect(mRect, mCornerRadius.toFloat(), mCornerRadius.toFloat(), mPaint)
    }

    override fun setAlpha(alpha: Int) {
        mPaint.alpha = alpha
    }

    override fun setColorFilter(colorFilter: ColorFilter?) {
        mPaint.colorFilter = colorFilter
    }

    override fun getOpacity(): Int {
        return PixelFormat.TRANSLUCENT
    }
}